<template>
  <div class="main">
    <van-nav-bar title="检测结果查询" left-arrow @click-left="$router.go(-1)" />
    <div class="content">
      <!-- 用户信息卡 -->
      <van-row class="user_card" v-if="defaultUser">
        <van-col span="24"
          ><div class="name_card">
            {{ defaultUser.name }}
            <span class="name_tag">{{ defaultUser.relation }}</span>
          </div></van-col
        >
        <van-col span="24"
          ><div class="id_card">
            {{ defaultUser.id_number | hidden }}
          </div></van-col
        >
        <div class="user_btn">
          <van-icon name="add" color="#1074FF" size="1.7rem" @click="userPop" />
          <div>切换就诊人</div>
        </div>
        <!-- 图标位置 -->
        <van-popup
          v-model="show"
          closeable
          close-icon-position="top-right"
          round
          position="bottom"
        >
          <div class="patient_list">
            <div style="text-align: center">选择就诊人</div>
            <div
              class="patient_content"
              v-for="(item, index) in patientList"
              :key="index"
            >
              <div class="patient_item" @click="switchPatient(index)">
                <van-col span="24" class="patient_item_single">
                  <div class="patient_name_card">{{ item.name }}</div>
                  <span class="name_tag">{{ item.relation }}</span>
                </van-col>
                <van-col span="24">
                  <div class="patient_id_card">
                    {{ item.id_number | hidden }}
                  </div>
                </van-col>
              </div>
            </div>
          </div>
        </van-popup>
      </van-row>
      <!-- 报告区 -->
      <div v-if="testDetail">
        <div class="report" v-for="(item, index) in testDetail" :key="index">
          <div class="report_title">
            新型冠状病毒核酸检测（咽拭子） <span>已完成</span>
          </div>
          <div class="report_detail">就诊科室 &nbsp; 保健科</div>
          <div class="report_detail">
            就诊时间 &nbsp;
            {{
              moment.unix(parseInt(item.appoint_time)).format("YYYY年MM月DD日")
            }}
          </div>
          <div class="report_detail">就诊人 &nbsp; {{ defaultUser.name }}</div>
          <div class="report_btn">
            <router-link
              :to="`/checkreport?pid=${patientid}&appoint_time=${item.appoint_time}`"
            >
              <button
                :disabled="item.appoint_time > Date.now() / 1000 ? true : false"
              >
                {{
                  item.appoint_time > Date.now() / 1000 ? "已预约" : "查看报告"
                }}
              </button>
            </router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      defaultUser: null,
      show: false,
      patientList: [],
      patientIndex: 0,
      patientid: 0,
      testDetail: [],
    };
  },
  mounted() {
    this.getDefaultPatient(this.patientIndex);
  },
  methods: {
    getDefaultPatient(value) {
      let id = sessionStorage.getItem("uid");
      // console.log('id-----'+id);
      let url = `${this.$base}patient/patient_list?uid=${id}`;
      this.axios.get(url).then((res) => {
        this.defaultUser = res.data.data[value];
        this.patientid = res.data.data[value].id;
        this.getReportDetial();
      });
    },
    userPop() {
      this.show = true;
      this.getPatientList();
    },
    getPatientList() {
      let id = sessionStorage.getItem("uid");
      // console.log('id-----'+id);
      let url = `${this.$base}patient/patient_list?uid=${id}`;
      this.axios.get(url).then((res) => {
        // console.log(res);
        this.patientList = res.data.data;
      });
    },
    switchPatient(value) {
      this.patientIndex = value;
      this.show = false;
      this.getDefaultPatient(value);
    },

    // 获取就诊人预约报告
    getReportDetial() {
      let url = `${this.$base}appointment/appointment_report?pid=${this.patientid}`;
      this.axios.get(url).then((res) => {
        // console.log(res.data.data);
        this.testDetail = res.data.data;
      });
    },
  },
  filters: {
    hidden: function (value) {
      return (value = value.replace(/^(.{4})(?:\d+)(.{4})$/, "$1******$2"));
    },
  },
};
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #1074ff;

  /deep/ .van-nav-bar__title {
    color: #fff;
  }
  /deep/ .van-icon-arrow-left {
    color: #fff;
  }
}
.content {
  width: 90%;
  margin: 0 auto;
  .user_card {
    position: relative;

    margin: 10px auto;
    background-color: #f5f9ff;
    border-radius: 5px;
    .name_card {
      margin-top: 20px;
      margin-left: 10px;
    }
    .name_tag {
      margin-left: 2vw;
      font-size: 3vw;
      padding: 1vw 2.5vw;
      color: #006eff;
      border-radius: 2.5vw;
      background-color: #e2edfe;
    }
    .id_card {
      margin-top: 10px;
      margin-left: 10px;
      margin-bottom: 20px;
      color: #aaaaaa;
      font-size: 14px;
    }
    .user_btn {
      width: 17vw;
      height: 7vw;
      text-align: center;
      position: absolute;
      right: 5vw;
      top: 50%;
      transform: translate(0, -80%);
      div {
        font-size: 2.33vw;
      }
    }
  }
}
.patient_list {
  margin-top: 4vw;
  // text-align: center;
  font-size: 4.33vw;
  .patient_item {
    width: 80%;
    margin: 0 auto;
    overflow: hidden;
    // background-color: #f5f9fe;
    box-shadow: 0 1px 1px #e3e3e3;
    user-select: cursor;
    .patient_item_single {
      display: flex;
      align-items: center;
      .patient_name_card {
        margin: 10px 0;
        align-items: flex-end;
        font-size: 3.5vw;
      }
      .name_tag {
        // position: absolute;
        // top: 2vw;
        // left: 4vw;
        margin-left: 2vw;
        font-size: 3vw;
        padding: 1vw 2.5vw;
        color: #006eff;
        border-radius: 2.5vw;
        background-color: #e2edfe;
      }
    }
    .patient_id_card {
      margin-top: 10px;
      // margin-left: 40px;
      margin-bottom: 5px;
      color: #393737;
      font-size: 14px;
      // border-bottom: 0.5px solid #aaaaaa;
    }
  }
}
.report {
  margin-top: 4vw;
  font-size: 4vw;
  position: relative;
  // border: 1px solid #aaaaaa;
  box-shadow: 0.5px 0 0.5px #aaaaaa;
  // border-radius: 4px;
  overflow: hidden;
  &::after {
    content: "";
    display: block;
    clear: both;
  }
  .report_title {
    margin-left: 2.33vw;
    margin-top: 3vw;
    color: #555555;
    span {
      font-size: 2.33vw;
      color: #1074ff;
      margin-left: 10vw;
    }
  }
  .report_detail {
    font-size: 2.33vw;
    color: #555555;
    margin-left: 2.33vw;
    margin-top: 1.5vw;
    margin-bottom: 1.5vw;
  }
  .report_btn {
    font-size: 2.33vw;
    float: right;
    button {
      border: none;
      height: 8vw;
      border-radius: 2px;
      outline: none;
      background-color: #1074ff;
      color: #fff;
      margin-right: 2.33vw;
      margin-bottom: 2vw;
    }
  }
}
</style>